package com.example.hehecat.yuejian1.mvp.presenter;

import com.example.hehecat.yuejian1.mvp.contract.MasterInfoContract;
import com.example.hehecat.yuejian1.mvp.model.MasterInfoModel;
import com.example.hehecat.yuejian1.mvp.model.bean.MasterLessonBean;
import com.example.hehecat.yuejian1.mvp.model.bean.SuccessBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;

public class MasterInfoPresenter implements MasterInfoContract.Presenter {
    private MasterInfoContract.View view;
    private MasterInfoContract.Model model;


    public MasterInfoPresenter(MasterInfoContract.View view) {
        this.view = view;
        model = new MasterInfoModel();
    }

    @Override
    public void collectMaster(int user_id, int master_id) {
        model.collectMaster(master_id, user_id, new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.body().getSuccess() == 1) {
                    view.showMsg("收藏成功");
                } else if(response.body().getSuccess() == 0) {
                    view.showMsg("取消收藏");
                } else {
                    view.showMsg(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void isCollected(int master_id, int user_id) {
        model.isMasterCollect(master_id, user_id, new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.body().getSuccess()==1) {
                    view.setCollect();
                }else if (response.body().getSuccess()==0) {
                    view.setUnCollect();
                }else {
                    view.showMsg("操作失败");
                }
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

            }
        });
    }


    @Override
    public void updateAdapterData(int master_id) {
        model.getLesson(master_id, new Subscriber<List<MasterLessonBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(List<MasterLessonBean> masterLessonBeans) {
                view.updateAdapterData(masterLessonBeans);
            }
        });
    }
}
